package com.monicash.monicash.base;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.XXPermissions;
import com.monicash.monicash.R;
import com.monicash.monicash.main.PermisosActivity;
import com.monicash.monicash.main.PrivacyActivity;
import com.monicash.monicash.util.T;
import com.monicash.monicash.widge.HeaderViewLayout;
import com.monicash.monicash.widge.LoadingAlertDialog;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseActivity extends FragmentActivity {

    public abstract Activity getActivity();

    public abstract void initView();
    public abstract void initData();

    public LoadingAlertDialog loadDingDialog;//加载dialog
    public final String ANIMAL_10="10";//默认动画
    public String animalStatus = ANIMAL_10;
    private boolean isFrontDesk = false;//是否处于前台显示 ，false表示不是前台显示
    public HeaderViewLayout headerViewLayout;
    public FrameLayout frameHeaderBackLayout, frameHeaderRightLayout;//返回，右边图片，右边文字
    public ImageView ivHeaderRight;//右边图标
    public TextView titleHeader, tvHeaderRight;//标题，右边的文字

    public List<String> permissionList = new ArrayList<>();

    public boolean isOpenSadTheme = true;//是否开启悲伤主题
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    public void init() {
        initHeaderView();
        initViewDada();
    }


    private void initHeaderView(){
        headerViewLayout = this.findViewById(R.id.base_header_framelayout);//如果我们include一个view并给他一个ID的话，会屏蔽这个头部的id，所以不能给include的参数赋予ID
        frameHeaderBackLayout = this.findViewById(R.id.base_header_framelayout_back);//返回
        frameHeaderRightLayout = this.findViewById(R.id.base_header_framelayout_right);//右边

        ivHeaderRight = this.findViewById(R.id.iv_baseheader_right_iv);//右边图片
        tvHeaderRight = this.findViewById(R.id.tv_baseheader_right_text);
        titleHeader = this.findViewById(R.id.tv_base_header_title);
        //返回按钮
        if (frameHeaderBackLayout!=null){
            frameHeaderBackLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackClick(v);
                }
            });
        }
        //右边按钮
        if (frameHeaderRightLayout!=null){
            frameHeaderRightLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onRightTitlClick(v);
                }
            });
        }

        if (headerViewLayout!=null){
            if (headerViewLayout.getParent() instanceof  FrameLayout || headerViewLayout.getParent() instanceof LinearLayout){
                setImmersionBarTitleBarView(headerViewLayout);
            }
        }
    }


    public void setTitleHeader(String value) {
        if (!TextUtils.isEmpty(value) && titleHeader != null) {
            titleHeader.setText(value);
        }
    }
    public void setPermissionList(List<String> list){
        permissionList.clear();
        permissionList.addAll(list);
    }


    public void setTitleHeader(int resource){
        if (resource > 0 && titleHeader != null){
            titleHeader.setText(this.getString(resource));
        }
    }


    public void setHeaderViewBackColor(int color){
        if (headerViewLayout!=null){
            headerViewLayout.setBackgroundColor(color);
        }
    }


    public void setTitleHeaderTextViewColor(int color){
        if (titleHeader!=null){
            titleHeader.setTextColor(color);
        }
    }


    public void setImageHeaderRight(int id) {
        if (frameHeaderRightLayout != null) {
            frameHeaderRightLayout.setVisibility(View.VISIBLE);
            ivHeaderRight.setImageResource(id);
        }
    }


    public void setImageHeaderRight(Drawable drawable) {
        if (frameHeaderRightLayout != null) {
            frameHeaderRightLayout.setVisibility(View.VISIBLE);
            ivHeaderRight.setImageDrawable(drawable);
        }
    }


    public void setHeaderRightText(String text) {
        if (frameHeaderRightLayout != null) {
            frameHeaderRightLayout.setVisibility(View.VISIBLE);
            tvHeaderRight.setText(text);
        }
    }


    public void setHeaderRightText(String text, int textColor) {
        if (frameHeaderRightLayout != null) {
            frameHeaderRightLayout.setVisibility(View.VISIBLE);
            tvHeaderRight.setTextColor(textColor);
            tvHeaderRight.setText(text);
        }
    }


    public void setOpenSadTheme(boolean openSadTheme) {
        isOpenSadTheme = openSadTheme;
    }

    @Override
    protected void onResume() {
        super.onResume();
        isFrontDesk = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isFrontDesk = false;
    }


    public void initViewDada(){
        initView();
        initData();
    }



    public void initImmersionBarStatusBarView(){
//        ImmersionBar.with(this).init();
    }




    public void setImmersionBarTitleBarView(View headerView){
//        ImmersionBar.with(this)
//                .titleBar(headerView)
//                .init();
    }



//    public void setImmersionBarTextDark(View headerView,boolean b,int color){
//        if (headerView!=null){
//            if (b){//设置字体为黑色
//                if (color!=0){
//                    ImmersionBar.with(this).titleBar(headerView).statusBarDarkFont(b,0.2f).statusBarColor(color).init();
//                }else {
//                    ImmersionBar.with(this).titleBar(headerView).statusBarDarkFont(b,0.2f).init();
//                }
//            }else {
//                if (color!=0){
//                    ImmersionBar.with(this).titleBar(headerView).statusBarDarkFont(false).statusBarColor(color).init();
//                }else {
//                    ImmersionBar.with(this).titleBar(headerView).statusBarDarkFont(false).init();
//                }
//
//            }
//        }else {
//            if (color!=0){
//                ImmersionBar.with(this).statusBarDarkFont(b, 0.2f).statusBarColor(color).init();
//            }else {
//                ImmersionBar.with(this).statusBarDarkFont(b, 0.2f).init();
//            }
//
//        }
//    }


    public void onBackClick(View view) {
        onBackPressed();
    }


    public void showSuccessToast(String txt) {
        T.showShort(this,txt);
    }


    public void showFailedToast(String txt) {
        T.showShort(this,txt);
    }


    public void normalToast(String txt){
        if (!TextUtils.isEmpty(txt)){
            T.showShort(this, txt);
        }
    }





    public void onRightTitlClick(View view){

    }



    public void showLoading() {
        if (loadDingDialog == null) {
            loadDingDialog = new LoadingAlertDialog(this);
        }
        if (!loadDingDialog.isShowing() && !isFinishing()) {//如果没有显示，就要显示
            loadDingDialog.show();
        }
    }


    public void hideLoading() {
        if (loadDingDialog != null && loadDingDialog.isShowing()) {
            loadDingDialog.dismiss();
        }
    }


    public void singleToStartActivity(Class classs){
        Intent intent = new Intent(this,classs);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }



    public void startActivity(Intent intent){
        startActivityForResult(intent,-1);
    }



    public void startActivity(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivityForResult(intent,-1);
    }


    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        if (bundle!=null){
            intent.putExtras(bundle);
        }
        startActivityForResult(intent,-1);
    }


    public void startActivityForResult(Class<?> cls, int requestCode) {
        Intent intent = new Intent(this, cls);
        startActivityForResult(intent, requestCode);
    }



    public void startActivityForResult(Class<?> cls, int requestCode, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        if (bundle!=null){
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }


    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        if (animalStatus.equals(ANIMAL_10)){
            overridePendingTransition(R.anim.activity_right_in,R.anim.activity_left_out);
        }
    }



    public boolean isFrontDesk() {
        return isFrontDesk;
    }




    public void loginChange(){
        //需要用户自己判断isLogin是否为1，1表示登录
    }

    public void closeSadTheme(){
        Paint paint = new Paint();
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(1);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        getWindow().getDecorView().setLayerType(View.LAYER_TYPE_HARDWARE,paint);
    }


    @Override
    public Resources getResources() { // 字体大小不跟随系统
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    public void getPermissions(){
        XXPermissions.with(this).permission(permissionList).request(new OnPermissionCallback() {
            @Override
            public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                if(allGranted){
                }
            }
        });
    }


    @Override
    public void finish() {
        super.finish();
        hideLoading();
        if (animalStatus.equals(ANIMAL_10)){
            overridePendingTransition(R.anim.activity_left_in, R.anim.activity_right_out);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (loadDingDialog!=null){
            if (loadDingDialog.isShowing()){
                loadDingDialog.dismiss();
            }
            loadDingDialog = null;
        }
    }

}
